package com.songlanyun.modules.goods.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.songlanyun.common.exception.RRException;
import com.songlanyun.common.supplychainapi.goods.model.GoodsDetailResult;
import com.songlanyun.modules.exception.GoodsExceptionEnum;
import com.songlanyun.modules.goods.model.values.SkuItem;
import com.songlanyun.modules.goods.model.vo.SkuComItemVo;
import org.redisson.client.RedisRedirectException;
import org.springframework.stereotype.Service;

import java.util.List;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.songlanyun.modules.goods.dao.GoodsSkuDao;
import com.songlanyun.modules.goods.entity.GoodsSku;
import com.songlanyun.modules.goods.service.GoodsSkuService;


@Service("goodsSkuService")
public class GoodsSkuServiceImpl extends ServiceImpl<GoodsSkuDao, GoodsSku> implements GoodsSkuService {

    /**
     * 清除商品的sku
     * @param goodsId
     */
    @Override
    public void clearByGoodsId(Long goodsId) {
        LambdaQueryWrapper<GoodsSku> wrapper =  new LambdaQueryWrapper<>();
        wrapper.eq(GoodsSku::getGoodsId,goodsId);
        this.baseMapper.delete(wrapper);
    }

    @Override
    public List<GoodsSku> loadGoodsSkuList(long goodsId) {

        return this.list(new LambdaQueryWrapper<GoodsSku>()
                .eq(GoodsSku::getGoodsId, goodsId));

    }



    /**
     * 批量创建sku
     * @param goodsId
     * @param list
     */
    @Override
    public void branchCreate(Long goodsId, List<GoodsSku> list) {
        if(list.size()>0)
        {
            clearByGoodsId(goodsId);
            list.forEach(o->{
                if(o.getMarketPrice().compareTo(o.getSalePrice())<0)
                {
                    throw new RRException(GoodsExceptionEnum.MARKET_PRICE_HEIGHT);
                }
                o.setGoodsId(goodsId);
            });
            /**
             * 检测单品只能被一个sku关联 sku更改成共用
            list.stream().forEach(goodsSku -> {
                validateGoodsSkuSingleGoodsRepeat(goodsSku);
            });
            */
            this.saveBatch(list);
        }
    }

    /**
     * 单品只能被一个sku关联
     * @param sku
     */
    private void validateGoodsSkuSingleGoodsRepeat(GoodsSku sku)
    {
        GoodsSku one = this.getOne(new LambdaQueryWrapper<GoodsSku>()
                .eq(GoodsSku::getShopId, sku.getShopId())
                .eq(GoodsSku::getSingleGoodsId, sku.getSingleGoodsId())
                .last("limit 1"));
        if(one!=null)
        {
            throw new RRException("单品："+sku.getSingleGoodsTitle()+",已被其它商品关联",GoodsExceptionEnum.SINGLE_GOODS_GOODS_SKU_UNIQUE.getCode());
        }

    }


    @Override
    public List<SkuComItemVo> loadSkuItemList(Long goodsId) {
        return this.baseMapper.loadSkuItemList(goodsId);
    }

    @Override
    public GoodsSku loadByGoodsAndSkuHashCode(Long goodsId, String skuHashCode) {
        GoodsSku sku = this.getOne(new LambdaQueryWrapper<GoodsSku>()
                .eq(GoodsSku::getGoodsId, goodsId)
                .eq(GoodsSku::getSkuHashCode, skuHashCode)
                .last("limit 1")
        );
        return sku;
    }

}
